package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.dao.TransferBackRuleSeniorDao;
import com.xbongbong.saas.domain.entity.TransferBackRuleSeniorEntity;
import com.xbongbong.saas.enums.ClueSeniorRuleTypeEnum;
import com.xbongbong.saas.enums.CustomerSeniorRuleTypeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.TransferBackRuleSeniorModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author yanglei
 * @ProjectName xbb-pro
 * @Description: 公海高级模式Model
 * @date 2022/4/13 10:59
 */
@Service
public class TransferBackRuleSeniorModelImpl implements TransferBackRuleSeniorModel {

    @Resource
    private TransferBackRuleSeniorDao transferBackRuleSeniorDao;

    @Override
    public Integer insert(TransferBackRuleSeniorEntity seniorRule) {
        long now = DateUtil.getInt();
        seniorRule.setAddTime(now);
        seniorRule.setUpdateTime(now);
        seniorRule.setDel(BasicConstant.ZERO);
        if (seniorRule.getId() == null || seniorRule.getId().equals(0L)) {
            return transferBackRuleSeniorDao.insert(seniorRule);
        }
        return transferBackRuleSeniorDao.update(seniorRule);
    }

    @Override
    public Integer update(TransferBackRuleSeniorEntity seniorRule) {
        return transferBackRuleSeniorDao.update(seniorRule);
    }

    @Override
    public void insertBatch(List<TransferBackRuleSeniorEntity> transferBackRuleSeniorEntityList) {
        transferBackRuleSeniorEntityList.forEach(item -> {
            long now = DateUtil.getInt();
            item.setAddTime(now);
            item.setUpdateTime(now);
            item.setDel(BasicConstant.ZERO);
        });
        transferBackRuleSeniorDao.insertBatch(transferBackRuleSeniorEntityList);
    }

    @Override
    public List<TransferBackRuleSeniorEntity> findEntitys(Map<String, Object> param) {
        return transferBackRuleSeniorDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return null;
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return transferBackRuleSeniorDao.deleteByKey(key,corpid);
    }

    @Override
    public TransferBackRuleSeniorEntity getByKey(Long key, String corpid) {
        return transferBackRuleSeniorDao.getByKey(key, corpid);
    }

    @Override
    public List<String> getSeniorRuleCorpidList(Long now, String startCorpid, Integer pageNum,Integer type,Integer businessType) {
        return transferBackRuleSeniorDao.getSeniorRuleCorpidList(now, startCorpid, pageNum,type,businessType);
    }

    @Override
    public List<TransferBackRuleSeniorEntity> getTransferBackRuleByType(String corpid,Long formId,Integer businessType,List<Integer> typeIn,Integer status) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("formId",formId);
        param.put("corpid",corpid);
        param.put("businessType",businessType);
        param.put("typeIn",typeIn);
        param.put("status",status);
        param.put("del", BasicConstant.ZERO);
        return findEntitys(param);
    }

    @Override
    public List<TransferBackRuleSeniorEntity> getRuleByType(String corpid, Integer businessType) throws XbbException{
        return transferBackRuleSeniorDao.getRuleByType(corpid, businessType);
    }

    @Override
    public Integer deleteByBusinessType(String corpid, Integer businessType) throws XbbException {
        return transferBackRuleSeniorDao.deleteByBusinessType(corpid, businessType);
    }

    /**
     * 获取最大拥有客户数，前负责人和捞取频率三个规则
     *
     * @param corpid
     * @return
     * @author jiangren.li
     * @date 2022-05-27 11:20
     */
    @Override
    public List<TransferBackRuleSeniorEntity> getSpecialRules(String corpid, Integer businessType) throws XbbException{
        List<Integer> ruleTypeIn = new ArrayList<>();
        if (Objects.equals(businessType, XbbRefTypeEnum.CLUE)) {
            ruleTypeIn.add(ClueSeniorRuleTypeEnum.MAX_CLUE_NUM_RULE.getCode());
            ruleTypeIn.add(ClueSeniorRuleTypeEnum.PRE_PRINCIPAL_RULE.getCode());
            ruleTypeIn.add(ClueSeniorRuleTypeEnum.GAIN_RATE_RULE.getCode());
        } else {
            ruleTypeIn.add(CustomerSeniorRuleTypeEnum.MAX_CUSTOMER_NUM_RULE.getCode());
            ruleTypeIn.add(CustomerSeniorRuleTypeEnum.PRE_PRINCIPAL_RULE.getCode());
            ruleTypeIn.add(CustomerSeniorRuleTypeEnum.GAIN_RATE_RULE.getCode());
        }

        if (CollectionsUtil.isNotEmpty(ruleTypeIn)) {
            //数据库查询，获取影响到获取客户判断的三个规则
            return getTransferBackRuleByType(corpid,null,businessType,ruleTypeIn,BasicConstant.ONE);
        }
        return null;
    }


}
